<?php

/**
* 登陆和注册,激活,找回密码模块
* 用户的注册接口
* 用户找回密码/激活接口
* /login/admin 管理员登陆地址
* /login/user 普通用户的登陆地址
*/
class login 
{
	private static $db;

	function __construct()
	{
		self::$db=M('m_bbs');	
	}
	/**
	 * 默认转到用户登录
	 */
	function index()
	{
		$this->user();
	}
	/**
	 * 管理员登陆页面
	 */
	function admin()
	{
		V('login/admin');
	}
	/**
	 * 用户登陆页面
	 */
	function user()
	{
		V('login/user');
	}
	
	private function createToken($userid,$username)
	{
		$info=Request::info();
		$token=md5($info['ip'].$info['ua'].$userid.$username.MIXKEY);
		return $token;
	}
	/**
	 * 标记已登录
	 */
	private function login($userid,$username)
	{	
		$token=$this->createToken($userid,$username);
		session("USERID",$userid);
		session("USERNAME",$username);
		return array('userid'=>$userid,'username'=>$username,'token'=>$token);
	}

	/**
	 * 管理员登陆接口
	 */
	function adminlogin()
	{

	}
	/**
	 * 用户登陆接口
	 */
	function userlogin()
	{
		
		$post=Request::filterPost(
				array(
						'name'=>array('require'=>'请填写用户名哦'),
						'pass'=>array('require'=>'请填写密码')
						), function($data){
				exit($data);
		});

		$ret=M('m_bbs')->loginUserByName($post['name'],$post['pass']);
		if($ret)
		{
			$userid=$ret[0]['id'];
			$msg=$this->login($userid,$post['name'],1);
			exit(json_encode(array('code'=>0,'msg'=>$msg)));
		}
		else
		{
			exit(json_encode(array('code'=>-1,'msg'=>'用户名或密码错误')));
		}
	

	}
	/**
	 * 用户注册接口
	 */
	function newuser()
	{
		$post=Request::filterPost(
			array(
				'email'=>array('email'=>'这不是一个邮箱吧','require'=>'邮箱是必填项哦'),
				'name'=>array('require'=>'先给自己起个用户名吧','username'=>'用户名字母数字汉字组成3到20位,不能全是数字'),
				'pass'=>array('require'=>'至少设置一个初始密码吧','password'=>'密码数字/大写字母/小写字母/标点符号组成，四种都必有，8位以上')
				),
				function($data){
					exit($data);
		});

		if(M('m_bbs')->userExistByNameOrEmail($post['name'],$post['email'])) //用户已被占用
		{
			exit(json_encode(array('code'=>-1,'msg'=>'用户名或邮箱已被占用')));
		}
		else
		{
			$userid=M('m_bbs')->createUser($post['name'],$post['pass'],$post['email']);
			if($userid)
			{
				$cookieInfo=$this->login($userid,$post['name']);
				if($this->sendActiveMail('1126045770@qq.com',$userid))
				{
					exit(json_encode(array('code'=>0,'msg'=>$cookieInfo)));
				}
				else
				{
					app::log('send active mail error at user '.$userid);
					exit(json_encode(array('code'=>-2,'msg'=>'send active mail error')));
				}
			}
			else
			{
				app::log('sql error when create user at pass'.$post['pass']);
				exit(json_encode(array('code'=>-2,'msg'=>'sql error when create user ')));
			}	
		}


	}
	/**
	 * 自动登录或者检查登陆
	 * token 包含userid,ip,ua
	 */
	function autologin()
	{
		$token=Request::cookie('token');
		$userid=Request::cookie('userid');
		$username=Request::cookie('username');
		$str=$this->createToken($userid,$username);
		if($str==$token) //cookie信息正确
		{
			$this->login($userid,$username);
			return true;
		}
		else
		{
			return false;
		}

	}
	/**
	 * 发送激活邮件
	 */
	private function sendActiveMail($to,$userid)
	{
		$ip=Request::ip();
		$time=time();
		$token=md5($ip.$userid.date('Ymd').MIXKEY); //连接当日有效,验证时根据id和time还有ip算出token验证
		$url='/bbs/active/'.$token.'?id='.$userid.'&t='.$time;
		$body=$this->bulidHtml(baseUrl($url));
		$subject=config('mailSubject');
		if(sendMail($to,$subject,$body))
		{
			return true;
		}
		return false;
	}
	/**
	 * 发信的正文
	 */
	private function bulidHtml($link)
	{
		 $sitename=SITENAME;
		 $now=date('Y-m-d H:i');
		 $body= "<meta charset='utf-8'><style>#active-mail{font:14px Comic Sans MS,'Microsoft Yahei',Arial;max-width:600px;height:auto;border-radius:10px;border:solid#0089e8 1px;margin:20px auto;-webkit-box-shadow:2px 2px 3px rgba(100,100,100,0.4);-moz-box-shadow:2px 2px 3px rgba(100,100,100,0.4);box-shadow:2px 2px 3px rgba(100,100,100,0.4)}#active-mail .top{max-width:600px;height:40px;background-color:#0089e8;-webkit-border-radius:10px 10px 0 0;-moz-border-radius:10px 10px 0 0;border-radius:10px 10px 0 0;line-height:40px;font-size:20px;color:#Fff;text-indent:20px}#active-mail .text{padding:20px;word-wrap:break-word}#active-mail .text p{text-indent:2em}</style><div id='active-mail'><div class='top'><b>请激活你的账户</b></div><div class='text'><b>尊敬的用户:</b><br><br><p>你好，你刚刚注册了{$sitename}的会员账户，请点击以下链接激活你的账户：</p><p><a href='{$link}'target='_blank'>{$link}</a></p><p>若以上链接无法点击,你还可以复制到地址栏访问!</p><p>此链接仅当天有效。</p><p>若不是你本人操作,请不必理会本邮件.</p><br><p align='right'>{$sitename}团队敬上</p><p align='right'>{$now}</p></div></div>";       
		 return $body; 
	} 

	function do_admin_logout()
	{

	}

	function do_user_logout()
	{
		
	}
}